package com.siruier.boss.common

import android.content.Context
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import com.alibaba.fastjson.JSON
import com.siruier.boss.BuildConfig
import com.siruier.boss.R
import com.siruier.boss.bean.OrderDetailBean
import com.siruier.boss.bean.UserLevelEnum
import com.siruier.boss.ui.ext.getString
import com.siruier.boss.ui.ext.gone
import com.siruier.boss.ui.ext.visible
import com.siruier.boss.ui.utils.Logger
import java.math.BigDecimal
import java.text.DecimalFormat

const val IS_GO_MAIN = "is_go_main"

const val ACTION_REQUEST = "ACTION_REQUEST"
const val ACTION_MAIN_CHANGE_TAB = "ACTION_MAIN_CHANGE_TAB"
const val ACTION_USER_INFO_CHANGE = "ACTION_USER_INFO_CHANGE"

const val PRIVACY_POLICY = "${BuildConfig.RELEASE_BASE_URL}privacy_policy.html"
const val AGREEMENT = "${BuildConfig.RELEASE_BASE_URL}agreement.html"
const val COMPANY_INTRODUCTION = "${BuildConfig.RELEASE_BASE_URL}company_introduction.html"
const val VIP_NOTE = "${BuildConfig.RELEASE_BASE_URL}vip_note.html"

const val MINI_PROGRAM_APP_ID = "wxe9a4ce09146e14e7"
const val MINI_PROGRAM_USER_NAME = "gh_c1c1af802983"

const val GOODS_SPAN_COUNT = 2

private val PRICE_FORMAT = DecimalFormat("0.##")
fun Double?.format() = this?.let { PRICE_FORMAT.format(this) } ?: ""
fun Double?.formatPrice() = this?.let { PRICE_FORMAT.format(this) } ?: ""
fun Double?.formatPriceRmb() = this?.let { "￥" + PRICE_FORMAT.format(this) } ?: ""
fun BigDecimal?.formatPrice() = this?.let { PRICE_FORMAT.format(this) } ?: ""
fun BigDecimal?.formatPriceRmb() = this?.let { "￥" + PRICE_FORMAT.format(this) } ?: ""
fun String.toArray(clazz: Class<*>) = JSON.parseArray(this, clazz)

fun OrderDetailBean.buildPrice(price: BigDecimal) = when (payType) {
    1 -> price.formatPrice()
    else -> price.formatPrice()
}

fun TextView.emptyGone(text: CharSequence, stringRes: Int = 0) = this.apply {
    if (text.isEmpty()) {
        gone()
    } else {
        this.text = if (stringRes != 0) getString(stringRes, text) else text
        visible()
    }
}

private val USER_LEVEL = UserLevelEnum.values()

fun getUserLevelEnum(userLevel: Int): UserLevelEnum {
    return USER_LEVEL.find { it.level == userLevel } ?: UserLevelEnum.MEMBER
}

val OrderDetailBean.orderType
    get() = when (payType) {
        1 -> "购物订单"
        2 -> "VIP升级订单"
        3 -> "拼团订单"
        else -> "购物订单"
    }


fun handleNickName(loginName: String?, nickName: String? = null): String {
    if (loginName == null) {
        return ""
    }
    if (!nickName.isNullOrEmpty()) {
        return nickName
    }
    return "BOSS-" + loginName.substring(loginName.length - 4, loginName.length)
}

fun handleLoginName(loginName: String?): String {
    if (loginName == null) {
        return ""
    }
    return if (loginName.length == 11) {
        loginName.mapIndexed { index, c ->
            if (index in 3..6) "*" else c
        }.joinToString("")
    } else {
        loginName
    }
}

class ADDividerItemDecoration(
    context: Context?,
    dividerId: Int = R.drawable.linear_layout_line,
    orientation: Int = VERTICAL,
) :
    DividerItemDecoration(context, orientation) {
    init {
        try {
            val mDivider = javaClass.superclass!!.getDeclaredField("mDivider")
            mDivider.isAccessible = true
            mDivider[this] = ContextCompat.getDrawable(context!!, dividerId)
            mDivider.isAccessible = false
        } catch (e: Exception) {
            Logger.e(e)
        }
    }
}